package com.shrct.businessprocess.purchase.impl;

import com.google.common.base.Objects;
import com.shrct.businessprocess.budget.RCCostBreakDown;
import com.shrct.businessprocess.organization.RCCEORoleProvider;
import com.shrct.businessprocess.organization.RCEmployee;
import com.shrct.businessprocess.organization.RCSalesLeaderRoleProvider;
import com.shrct.businessprocess.organization.impl.RCEmployeeImpl;
import com.shrct.businessprocess.project.RCProject;
import com.shrct.businessprocess.project.RCProjectMoney;
import com.shrct.businessprocess.project.RCProjectProcessEvent;
import com.shrct.businessprocess.project.RCProjectProcessEventStatus;
import com.shrct.businessprocess.project.RCProjectStat;
import com.shrct.businessprocess.project.impl.RCProjectProcessEventImpl;
import com.shrct.businessprocess.purchase.RCBasePurchaseRequest;
import com.shrct.businessprocess.purchase.RCHasPurchaseBudget;
import com.shrct.businessprocess.purchase.RCProductPurchaseRequestStatus;
import com.shrct.businessprocess.purchase.RCPurchaseRequestProcess;
import com.shrct.businessprocess.purchase.impl.RCBasePurchaseRequestImpl;
import com.shrct.businessprocess.purchase.proxy.RCPurchaseRequestProcessProxy;
import com.uniquesoft.uidl.validation.Validates;
import edu.fudan.langlab.domain.organization.Party;
import edu.fudan.langlab.domain.security.ISystemUserService;
import edu.fudan.langlab.domain.workflow.ActivityNodeDef;
import edu.fudan.langlab.domain.workflow.Token;
import edu.fudan.langlab.domain.workflow.impl.BusinessProcessImpl;
import edu.fudan.mylang.common.StringExtensions;
import edu.fudan.mylang.pf.IGenericQuery;
import edu.fudan.mylang.pf.IObjectFactory;
import edu.fudan.mylang.utils.DateUtil;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.springframework.beans.factory.BeanFactory;

@Entity
@DiscriminatorValue("RCPURCHASEREQUESTPROCESS_TYPE")
public class RCPurchaseRequestProcessImpl extends BusinessProcessImpl implements RCPurchaseRequestProcess {
  public RCPurchaseRequestProcessImpl() {
    super();
  }
  
  public String getProcessName() {
    return "采购申请流程";
  }
  
  public String getProcessType() {
    return "RCPurchaseRequestProcess";
  }
  
  public ActivityNodeDef getActivityDef(final String activityId) {
    edu.fudan.langlab.domain.workflow.ActivityNodeDef result = new edu.fudan.langlab.domain.workflow.ActivityNodeDef();
    result.setActivityId(activityId);
    if("__end__".equals(activityId)){
    	result.setDescription("end");
    }else
    	result.setDescription(Enum.valueOf(com.shrct.businessprocess.purchase.RCPurchaseRequestProcessStatus.class, activityId).toString());
    return result;
    
  }
  
  public void _start(final RCBasePurchaseRequest purchaseRequest, final Token token) {
    BeanFactory _beanFactory = this.getBeanFactory();
    ISystemUserService _bean = _beanFactory.<ISystemUserService>getBean(ISystemUserService.class);
    Party _currentUserAsParty = _bean.getCurrentUserAsParty();
    this.starter = ((RCEmployee) _currentUserAsParty);
    this.purchaseRequest = purchaseRequest;
    this.purchaseRequest.setStatus(RCProductPurchaseRequestStatus.auditing);
    RCHasPurchaseBudget _budget = purchaseRequest.getBudget();
    RCCostBreakDown _cbd = _budget.getCbd();
    RCProject _project = _cbd.getProject();RCProjectProcessEvent rCProjectProcessEvent=RCProjectProcessEventImpl.create(_project,getObjectFactory());
    final Procedure1<RCProjectProcessEvent> _function = new Procedure1<RCProjectProcessEvent>() {
        public void apply(final RCProjectProcessEvent it) {
          it.setBp(RCPurchaseRequestProcessImpl.this);
          RCProjectMoney _totalRequestedAmount = purchaseRequest.getTotalRequestedAmount();
          String _string = _totalRequestedAmount.toString();
          String _plus = ("\u91C7\u8D2D\u7533\u8BF7,\u91C7\u8D2D\u9884\u7B97\u91D1\u989D:" + _string);
          it.setDescription(_plus);
          it.setStatus(RCProjectProcessEventStatus.executing);
          Date _day = DateUtil.today();
          it.setDate(_day);
        }
      };
    ObjectExtensions.<RCProjectProcessEvent>operator_doubleArrow(rCProjectProcessEvent, _function);
  }
  
  public void start(final RCBasePurchaseRequest purchaseRequest) {
    edu.fudan.langlab.domain.workflow.Token token = getProcessManager().start(this,com.shrct.businessprocess.purchase.RCPurchaseRequestProcessStatus.SaleManagerAudit.name());
    _start(purchaseRequest,token);
    getProcessManager().registTask(this, token, saleManager(),getSaleManagerAudit_task_message());
    systemExecute();
    String msg = getStart_log_message(purchaseRequest);
    getProcessManager().createActivity(this,"Create Process",msg);
    
    
  }
  
  public String getStart_log_message(final RCBasePurchaseRequest purchaseRequest) {
    return "\u542F\u52A8\u91C7\u8D2D\u7533\u8BF7\u6D41\u7A0B";
  }
  
  public void SaleModify() {
  }
  
  public void _SaleModify_modified(final String comment, final Token token) {
    Token SaleManagerAudit_token = getProcessManager().forwardTo(token,com.shrct.businessprocess.purchase.RCPurchaseRequestProcessStatus.SaleManagerAudit.name());
    getProcessManager().registTask(this, SaleManagerAudit_token,saleManager(),getSaleManagerAudit_task_message());
  }
  
  public void SaleModify_modified(final String comment) {
    edu.fudan.langlab.domain.workflow.Token token = getProcessManager().assertToken(this,com.shrct.businessprocess.purchase.RCPurchaseRequestProcessStatus.SaleModify.name());
    _SaleModify_modified(comment,token);
    systemExecute();
    String msg = getSaleModify_Modified_log_message(comment);
    getProcessManager().createActivity(this,"销售修改采购申请.销售修改请购信息",msg);
    
    
  }
  
  public String getSaleModify_Modified_log_message(final String comment) {
    String _nullsafe = StringExtensions.nullsafe(comment);
    String _plus = ("\u9500\u552E\u4FEE\u6539\u8BF7\u8D2D\u4FE1\u606F,\u5907\u6CE8:" + _nullsafe);
    return _plus;
  }
  
  public void _SaleModify_cancel(final String comment, final Token token) {
    this.purchaseRequest.setStatus(RCProductPurchaseRequestStatus.canceled);
    Token __end___token = getProcessManager().forwardTo(token,"__end__");
    
  }
  
  public void SaleModify_cancel(final String comment) {
    edu.fudan.langlab.domain.workflow.Token token = getProcessManager().assertToken(this,com.shrct.businessprocess.purchase.RCPurchaseRequestProcessStatus.SaleModify.name());
    _SaleModify_cancel(comment,token);
    systemExecute();
    String msg = getSaleModify_Cancel_log_message(comment);
    getProcessManager().createActivity(this,"销售修改采购申请.销售取消请购",msg);
    
    
  }
  
  public String getSaleModify_Cancel_log_message(final String comment) {
    String _nullsafe = StringExtensions.nullsafe(comment);
    String _plus = ("\u9500\u552E\u53D6\u6D88\u8BF7\u8D2D,\u5907\u6CE8:" + _nullsafe);
    return _plus;
  }
  
  public String getSaleModify_task_message() {
    RCHasPurchaseBudget _budget = this.purchaseRequest.getBudget();
    RCCostBreakDown _cbd = _budget.getCbd();
    RCProject _project = _cbd.getProject();
    String _code = _project.getCode();
    String _plus = ("\u9879\u76EE\u7F16\u53F7:" + _code);
    return _plus;
  }
  
  public void SaleManagerAudit() {
  }
  
  public void _SaleManagerAudit_reject(final String comment, final Token token) {
    Token SaleModify_token = getProcessManager().forwardTo(token,com.shrct.businessprocess.purchase.RCPurchaseRequestProcessStatus.SaleModify.name());
    getProcessManager().registTask(this, SaleModify_token,saler(),getSaleModify_task_message());
  }
  
  public void SaleManagerAudit_reject(final String comment) {
    edu.fudan.langlab.domain.workflow.Token token = getProcessManager().assertToken(this,com.shrct.businessprocess.purchase.RCPurchaseRequestProcessStatus.SaleManagerAudit.name());
    _SaleManagerAudit_reject(comment,token);
    systemExecute();
    String msg = getSaleManagerAudit_Reject_log_message(comment);
    getProcessManager().createActivity(this,"总经理审批助理审批.总经理审批助理拒绝",msg);
    
    
  }
  
  public String getSaleManagerAudit_Reject_log_message(final String comment) {
    String _plus = ("\u603B\u7ECF\u7406\u5BA1\u6279\u52A9\u7406\u62D2\u7EDD,\u7406\u7531:" + comment);
    return _plus;
  }
  
  public void _SaleManagerAudit_agree(final String comment, final Token token) {
    Token CEOAudit_token = getProcessManager().forwardTo(token,com.shrct.businessprocess.purchase.RCPurchaseRequestProcessStatus.CEOAudit.name());
    getProcessManager().registTask(this, CEOAudit_token,ceo(),getCEOAudit_task_message());
  }
  
  public void SaleManagerAudit_agree(final String comment) {
    edu.fudan.langlab.domain.workflow.Token token = getProcessManager().assertToken(this,com.shrct.businessprocess.purchase.RCPurchaseRequestProcessStatus.SaleManagerAudit.name());
    _SaleManagerAudit_agree(comment,token);
    systemExecute();
    String msg = getSaleManagerAudit_Agree_log_message(comment);
    getProcessManager().createActivity(this,"总经理审批助理审批.总经理审批助理同意",msg);
    
    
  }
  
  public String getSaleManagerAudit_Agree_log_message(final String comment) {
    String _xifexpression = null;
    boolean _isNullOrEmpty = org.eclipse.xtext.xbase.lib.StringExtensions.isNullOrEmpty(comment);
    if (_isNullOrEmpty) {
      _xifexpression = "";
    } else {
      String _plus = (",\u5907\u6CE8:" + comment);
      _xifexpression = _plus;
    }
    String _plus_1 = ("\u603B\u7ECF\u7406\u5BA1\u6279\u52A9\u7406\u540C\u610F" + _xifexpression);
    return _plus_1;
  }
  
  public String getSaleManagerAudit_task_message() {
    RCHasPurchaseBudget _budget = this.purchaseRequest.getBudget();
    RCCostBreakDown _cbd = _budget.getCbd();
    RCProject _project = _cbd.getProject();
    String _code = _project.getCode();
    String _plus = ("\u9879\u76EE\u7F16\u53F7:" + _code);
    return _plus;
  }
  
  public void CEOAudit() {
  }
  
  public void _CEOAudit_reject(final String comment, final Token token) {
    Token SaleManagerAudit_token = getProcessManager().forwardTo(token,com.shrct.businessprocess.purchase.RCPurchaseRequestProcessStatus.SaleManagerAudit.name());
    getProcessManager().registTask(this, SaleManagerAudit_token,saleManager(),getSaleManagerAudit_task_message());
  }
  
  public void CEOAudit_reject(final String comment) {
    edu.fudan.langlab.domain.workflow.Token token = getProcessManager().assertToken(this,com.shrct.businessprocess.purchase.RCPurchaseRequestProcessStatus.CEOAudit.name());
    _CEOAudit_reject(comment,token);
    systemExecute();
    String msg = getCEOAudit_Reject_log_message(comment);
    getProcessManager().createActivity(this,"总经理审批.总经理拒绝",msg);
    
    
  }
  
  public String getCEOAudit_Reject_log_message(final String comment) {
    String _plus = ("\u603B\u7ECF\u7406\u62D2\u7EDD,\u7406\u7531:" + comment);
    return _plus;
  }
  
  public void _CEOAudit_agree(final String comment, final Token token) {
    IGenericQuery<RCProjectProcessEvent> query=getObjectFactory().createGenericQuery(RCProjectProcessEvent.class,"select e from com.shrct.businessprocess.project.impl.RCProjectProcessEventImpl as e   where ((e.bp) = (:this)) and  e.active = 1      ").setParameter("this",this);
    RCProjectProcessEvent p = query.uniqueResult();
    final Procedure1<RCProjectProcessEvent> _function = new Procedure1<RCProjectProcessEvent>() {
        public void apply(final RCProjectProcessEvent it) {
          RCProjectMoney _expectCost = RCPurchaseRequestProcessImpl.this.purchaseRequest.getExpectCost();
          String _string = _expectCost.toString();
          String _plus = ("\u91C7\u8D2D\u7533\u8BF7\u5BA1\u6279\u901A\u8FC7,\u7533\u8BF7\u91D1\u989D:" + _string);
          it.setDescription(_plus);
          Date _day = DateUtil.today();
          it.setFinishDate(_day);
          it.setStatus(RCProjectProcessEventStatus.finished);
        }
      };
    ObjectExtensions.<RCProjectProcessEvent>operator_doubleArrow(p, _function);
    RCHasPurchaseBudget _budget = this.purchaseRequest.getBudget();
    RCCostBreakDown _cbd = _budget.getCbd();
    RCProject _project = _cbd.getProject();
    RCProjectStat _stat = _project.getStat();
    _stat.updateInfo();
    this.purchaseRequest.auditPassed();
    this.purchaseRequest.setStatus(RCProductPurchaseRequestStatus.executing);
    Token __end___token = getProcessManager().forwardTo(token,"__end__");
    
  }
  
  public void CEOAudit_agree(final String comment) {
    edu.fudan.langlab.domain.workflow.Token token = getProcessManager().assertToken(this,com.shrct.businessprocess.purchase.RCPurchaseRequestProcessStatus.CEOAudit.name());
    _CEOAudit_agree(comment,token);
    systemExecute();
    String msg = getCEOAudit_Agree_log_message(comment);
    getProcessManager().createActivity(this,"总经理审批.总经理同意",msg);
    
    
  }
  
  public String getCEOAudit_Agree_log_message(final String comment) {
    String _nullsafe = StringExtensions.nullsafe(comment);
    String _plus = ("\u603B\u7ECF\u7406\u540C\u610F,\u5907\u6CE8:" + _nullsafe);
    return _plus;
  }
  
  public String getCEOAudit_task_message() {
    RCHasPurchaseBudget _budget = this.purchaseRequest.getBudget();
    RCCostBreakDown _cbd = _budget.getCbd();
    RCProject _project = _cbd.getProject();
    String _code = _project.getCode();
    String _plus = ("\u9879\u76EE\u7F16\u53F7:" + _code);
    return _plus;
  }
  
  @JoinColumn(name = "purchaseRequest_id")
  @ManyToOne(targetEntity = RCBasePurchaseRequestImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCBasePurchaseRequest purchaseRequest;
  
  public RCBasePurchaseRequest getPurchaseRequest() {
    return this.purchaseRequest;
  }
  
  public RCPurchaseRequestProcess setPurchaseRequest(final RCBasePurchaseRequest purchaseRequest) {
    this.purchaseRequest = purchaseRequest;
    return this;			
    
  }
  
  @JoinColumn(name = "starter_id")
  @ManyToOne(targetEntity = RCEmployeeImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCEmployee starter;
  
  public RCEmployee getStarter() {
    return this.starter;
  }
  
  public RCPurchaseRequestProcess setStarter(final RCEmployee starter) {
    this.starter = starter;
    return this;			
    
  }
  
  public boolean _systemExecute() {
    boolean stateChanged = false;
    return stateChanged;
  }
  
  public static RCPurchaseRequestProcess create(final IObjectFactory objectFactory) {
    com.shrct.businessprocess.purchase.RCPurchaseRequestProcess rCPurchaseRequestProcess = new com.shrct.businessprocess.purchase.impl.RCPurchaseRequestProcessImpl();
    objectFactory.create(rCPurchaseRequestProcess);
    return rCPurchaseRequestProcess;			
    
  }
  
  public Party saler() {
    return this.starter;
  }
  
  public Party ceo() {
    RCEmployee _xblockexpression = null;
    {
      BeanFactory _beanFactory = this.getBeanFactory();
      RCCEORoleProvider _bean = _beanFactory.<RCCEORoleProvider>getBean(RCCEORoleProvider.class);
      final RCEmployee res = _bean.getParty();
      boolean _notEquals = (!Objects.equal(res, null));
      Validates.Assert(_notEquals, "\u672A\u8BBE\u7F6E\u603B\u7ECF\u7406");
      _xblockexpression = (res);
    }
    return _xblockexpression;
  }
  
  public Party saleManager() {
    RCEmployee _xblockexpression = null;
    {
      BeanFactory _beanFactory = this.getBeanFactory();
      RCSalesLeaderRoleProvider _bean = _beanFactory.<RCSalesLeaderRoleProvider>getBean(RCSalesLeaderRoleProvider.class);
      final RCEmployee res = _bean.getParty();
      boolean _notEquals = (!Objects.equal(res, null));
      Validates.Assert(_notEquals, "\u672A\u8BBE\u7F6E\u603B\u7ECF\u7406\u5BA1\u6279\u52A9\u7406");
      _xblockexpression = (res);
    }
    return _xblockexpression;
  }
  
  public RCPurchaseRequestProcessProxy toProxy() {
    com.shrct.businessprocess.purchase.proxy.RCPurchaseRequestProcessProxy proxy = new com.shrct.businessprocess.purchase.proxy.RCPurchaseRequestProcessProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
